/*
 * $Id: Main.java 191 2009-06-11 14:43:58Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.apps.pictureviewer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JPanel;

import deltadesk.apps.pictureviewer.data.FilePictureModel;
import deltadesk.apps.pictureviewer.util.DisplayUtil;
import deltadesk.apps.pictureviewer.widget.Box;
import deltadesk.apps.pictureviewer.widget.InfoBar;
import deltadesk.apps.pictureviewer.widget.PictureViewer;

/**
 * Main
 * 
 * @author Andreas Meyer
 * @version $Revision: 191 $
 */
public class Main {
	
	/** The application version. */
	public static final String VERSION = "V0.3";

	/** The top info bar. */
	private final InfoBar infoBar = new InfoBar();

	/** The picture viewer component. */
	private PictureViewer viewer;

	/** The picture model. */
	private FilePictureModel model;

	/** The current index in the model. */
	private int index;


	/**
	 * Construct the main class.
	 * 
	 * @param dirStr
	 * @throws IOException 
	 */
	public Main(final String dirStr) throws IOException {

		final FullscreenWindow window = new FullscreenWindow();

		final JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		contentPanel.setBackground(Color.BLACK);
		DisplayUtil.hideMouseCursor(contentPanel);
		window.show(0, "DeltaDesk Picture Viewer", contentPanel);
		
		final Box infoBarBox = new Box();
		infoBarBox.add(infoBar);
		infoBarBox.setPreferredSize(new Dimension(1, 25));
		infoBarBox.setBorder(null);
		contentPanel.add(infoBarBox, BorderLayout.NORTH);
		infoBar.setText1("DeltaDesk Picture Viewer " + VERSION);

		final Box buttonBarBox = new Box();
		buttonBarBox.setPreferredSize(new Dimension(1, 25));
		buttonBarBox.setBorder(null);
		contentPanel.add(buttonBarBox, BorderLayout.SOUTH);

		contentPanel.validate();


		model = new FilePictureModel(new File(dirStr));
		viewer = new PictureViewer(model);
		contentPanel.add(viewer, BorderLayout.CENTER);

		index = model.init();
		setIndex();
		contentPanel.validate();

		
		window.addKeyListener(new KeyAdapter() {

			public void keyPressed(final KeyEvent ke) {
				
				// next
				if(ke.getKeyCode() == KeyEvent.VK_SPACE ||
				   ke.getKeyCode() == KeyEvent.VK_RIGHT) {
					
					if(index+1 < model.getSize()) index++;
					setIndex();
					
				// previous
				} else if(ke.getKeyCode() == KeyEvent.VK_BACK_SPACE ||
						   ke.getKeyCode() == KeyEvent.VK_LEFT) {
					
					if(index > 0) index--;
					setIndex();

				// page down
				} else if(ke.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
					
					index += 10;
					if(index >= model.getSize()) {
						index = model.getSize() - 1;
					}
					setIndex();

					// page up
				} else if(ke.getKeyCode() == KeyEvent.VK_PAGE_UP) {
					
					index -= 10;
					if(index < 0) {
						index = 0;
					}
					setIndex();

				// home
				} else if(ke.getKeyCode() == KeyEvent.VK_HOME) {
					
					index = 0;
					setIndex();

				// end
				} else if(ke.getKeyCode() == KeyEvent.VK_END) {
					
					index = model.getSize() - 1;
					setIndex();

				// toggle visible state for bars
				} else if(ke.getKeyCode() == KeyEvent.VK_B) {
					
					infoBarBox.setVisible(!infoBarBox.isVisible());
					buttonBarBox.setVisible(!buttonBarBox.isVisible());

					// toggle scale up
				} else if(ke.getKeyCode() == KeyEvent.VK_S) {
					
					viewer.setScaleUp(!viewer.isScaleUp());
					
				// toggle scale up
				} else if(ke.getKeyCode() == KeyEvent.VK_F) {
					
					window.toggleFullscreen();

				// exit
				} else if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
					System.exit(0);
				}
			}
		});

	}
	
	/**
	 * Set index for picture viewer component.
	 * 
	 * @param file
	 */
	private void setIndex() {
		
		viewer.setIndex(index);
		
		final String text1 = String.valueOf(index+1);
		final String text2 = String.valueOf(model.getSize());
		String text3;
		try {
			text3 = model.getPicture(index).getSource().toString();
		} catch (IOException e) {
			text3 = "Failed reading image";
		}
		
		infoBar.setText2("[" + text1 + "/" + text2 + "] " + text3);
		
		infoBar.repaint();
	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		if (args.length > 0) {
			new Main(args[0]);
		} else {
			System.out.println("Usage: deltadesk-pictureviewer <filename>");
		}

	}

}
